/*
 * This file is part of JContextFree.
 *
 * JContextFree is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JContextFree is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JContextFree.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.enricod.jcontextfree.gui.paint.java2d;

import it.enricod.jcontextfree.engine.model.IFigure;
import it.enricod.jcontextfree.engine.model.IFigureParamValues;
import it.enricod.jcontextfree.gui.paint.IFigurePainter;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;

import org.apache.log4j.Logger;

public class CircleFigurePainter implements IFigurePainter {

    private IFigure figure;
    private Logger logger = Logger.getLogger("CircleFigurePainter");

    public CircleFigurePainter(IFigure figure) {
        this.figure = figure;
    }

    public void draw(BufferedImage image) {
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        
        IFigureParamValues absoluteParams = figure.getParameters().getAbsolute();
        
        AffineTransform oldTransform = g.getTransform();
        AffineTransform transform = new AffineTransform();
        double s = absoluteParams.getScale();
        double x1 = figure.getFigureRegion().getP1().getX();
        double y1 = figure.getFigureRegion().getP1().getY();

        // trasliamo in alto in modo che la figura sia corretta sul video
        transform.translate(x1, image.getHeight() - y1 - s);
        g.setTransform(transform);

        g.setPaint(Color.BLACK);

        g.fill(new Ellipse2D.Double(0, 0, (double) s, (double) s));
        g.setTransform(oldTransform);
    }

    public IFigure getFigure() {
        return figure;
    }
}
